﻿using System;
using System.IO;
using EnvDTE;
using EnvDTE80;
using Extensibility;
using Microsoft.VisualStudio.CommandBars;
using System.Resources;
using System.Reflection;
using System.Globalization;
using System.Collections.Generic;
using System.Windows.Forms;

namespace dotNails {
	public class MenuManager {
		#region Properties
		private DTE2 _application;
		private List<CommandBarEvents> _menuItemHandlerList = new List<CommandBarEvents>();
		private Dictionary<string, CommandBase> _cmdList = new Dictionary<string, CommandBase>();
		#endregion Properties

		#region Constructors
		public MenuManager(DTE2 application) {
			_application = application;
		}
		#endregion Constructors

		#region Events
		/// <summary>
		/// Handles a click of the menu item
		/// </summary>
		/// <param name="commandBarControl">The command bar control.</param>
		/// <param name="handled">if set to <c>true</c> [handled].</param>
		/// <param name="cancelDefault">if set to <c>true</c> [cancel default].</param>
		private void MenuItem_Click(object commandBarControl, ref bool handled, ref bool cancelDefault) {
			try {
				// We perform the command only if we found the command corresponding to the menu item clicked
				CommandBarControl menuItem = (CommandBarControl)commandBarControl;
				if (_cmdList.ContainsKey(menuItem.Tag)) {
					_cmdList[menuItem.Tag].Perform();
				}
			}
			catch (Exception e) {
				MessageBox.Show(e.Message, "CodeOMatic AddIn");
			}
		}
		#endregion Events

		#region Public Methods
		/// <summary>
		/// Creates the popup menu.
		/// </summary>
		/// <param name="commandBarName">Name of the command bar.</param>
		/// <param name="menuName">Name of the menu.</param>
		/// <returns></returns>
		public CommandBarPopup CreatePopupMenu(string commandBarName, string menuName) {
			CommandBar commandBar = ((CommandBars)_application.DTE.CommandBars)[commandBarName];
			CommandBarPopup menu = commandBar.Controls.Add(MsoControlType.msoControlPopup, Missing.Value, Missing.Value, 1, true) as CommandBarPopup;
			menu.Caption = menuName;
			menu.TooltipText = "";
			return menu;
		}

		/// <summary>
		/// Creates the popup menu.
		/// </summary>
		/// <param name="popupMenu">The popup menu.</param>
		/// <param name="subPopupMenuName">Name of the sub popup menu.</param>
		/// <param name="position">The position.</param>
		public CommandBarPopup CreatePopupMenu(CommandBarPopup popupMenu, string subPopupMenuName, int position) {
			CommandBarPopup menu = (CommandBarPopup)popupMenu.Controls.Add(MsoControlType.msoControlPopup, 1, "", position, true);
			menu.Caption = subPopupMenuName;
			menu.TooltipText = "";
			return menu;
		}

		/// <summary>
		/// Adds the command.
		/// </summary>
		/// <param name="menu">The popup menu.</param>
		/// <param name="cmd">The command.</param>
		/// <param name="position">The position in the menu.</param>
		public void AddCommandMenu(CommandBarPopup popupMenu, CommandBase cmd, int position) {
			CommandBarControl menuItem = AddMenuItem(popupMenu, cmd, position);
			AddClickEventHandler(menuItem);
			AddCommandToList(cmd);
		}

		public void ClearPopupMenu(CommandBarPopup popupMenu, int startPosition) {
			try {
				int itemCount = popupMenu.Controls.Count;
				for (int i = startPosition; i <= itemCount; i++) {
					popupMenu.Controls[startPosition].Delete(null);
				}
			}
			catch (Exception ex) {
				throw;
			}

		}
		#endregion Public Methods

		#region Private Methods
		/// <summary>
		/// Add the menu item to the popup menu
		/// </summary>
		/// <param name="menu">The menu.</param>
		/// <param name="cmd">The CMD.</param>
		/// <param name="position">The position.</param>
		/// <returns></returns>
		private CommandBarControl AddMenuItem(CommandBarPopup menu, CommandBase cmd, int position) {
			CommandBarControl menuItem = menu.Controls.Add(MsoControlType.msoControlButton, 1, "", position, true);
			menuItem.Tag = cmd.ID.ToString();
			menuItem.Caption = cmd.Caption;
			menuItem.TooltipText = cmd.TooltipText;
			return menuItem;
		}

		/// <summary>
		/// Adds handler to the menu item click event.
		/// </summary>
		/// <param name="menuItem">The menu item.</param>
		private void AddClickEventHandler(CommandBarControl menuItem) {
			CommandBarEvents menuItemHandler = (EnvDTE.CommandBarEvents)_application.DTE.Events.get_CommandBarEvents(menuItem);
			menuItemHandler.Click += new _dispCommandBarControlEvents_ClickEventHandler(MenuItem_Click);
			_menuItemHandlerList.Add(menuItemHandler);
		}

		/// <summary>
		/// Adds the command to list.
		/// </summary>
		/// <param name="cmd">The CMD.</param>
		private void AddCommandToList(CommandBase cmd) {
			if (!_cmdList.ContainsKey(cmd.ID.ToString())) {
				_cmdList.Add(cmd.ID.ToString(), cmd);
			}
		}
		#endregion Private Methods

	}
}
